package com.quick.controller.system;

import java.util.List;

import com.quick.common.core.domain.Select;
import com.quick.common.utils.StringUtils;
import com.quick.system.domain.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.system.domain.SysDept;
import com.quick.system.service.SysDeptService;
import com.quick.common.utils.poi.ExcelUtil;

/**
 * 部门Controller
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Api(value = "部门管理", tags = {"部门操作接口"})
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {
    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ps.hasPermission('system:dept:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询部门列表")
    public ApiResponse list(SysDept sysDept) {
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        return total(list);

    }

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ps.hasPermission('system:dept:list')")
    @GetMapping("/list/exclude/{deptId}")
    public ApiResponse excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<SysDept> list = sysDeptService.selectSysDeptList(new SysDept());
        list.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), String.valueOf(deptId)));
        return success(list);
    }

    /**
     * 导出部门列表
     */
    @PreAuthorize("@ps.hasPermission('system:dept:export')")
    @Log(title = "部门", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出部门列表")
    public void export(HttpServletResponse response, SysDept sysDept) {
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        ExcelUtil<SysDept> util = new ExcelUtil<>(SysDept.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    @PreAuthorize("@ps.hasPermission('system:dept:query')")
    @GetMapping(value = "/{deptId}")
    @ApiOperation(value = "获取部门详细信息")
    public ApiResponse getInfo(@PathVariable("deptId") Long deptId) {
        return success(sysDeptService.selectSysDeptByDeptId(deptId));
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ps.hasPermission('system:dept:add')")
    @Log(title = "部门", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增部门")
    public ApiResponse add(@RequestBody SysDept sysDept) {
        return success(sysDeptService.insertSysDept(sysDept));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ps.hasPermission('system:dept:edit')")
    @Log(title = "部门", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改部门")
    public ApiResponse edit(@RequestBody SysDept sysDept) {
        return success(sysDeptService.updateSysDept(sysDept));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ps.hasPermission('system:dept:remove')")
    @Log(title = "部门", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{deptId}")
    @ApiOperation(value = "删除部门")
    public ApiResponse remove(@PathVariable Long deptId) {
        return success(sysDeptService.deleteSysDeptByDeptId(deptId));
    }

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ps.hasPermission('system:dept:list')")
    @GetMapping("/option")
    @ApiOperation(value = "查询部门列表")
    public ApiResponse option() {
        List<SysDept> list = sysDeptService.selectSysDeptList(new SysDept());
        List<Select> selectRoles = Select.getSelect();
        for (SysDept sysDept : list) {
            selectRoles.add(new Select(sysDept.getDeptName(), sysDept.getDeptId()));
        }
        return success(selectRoles);
    }
}
